package com.lablzh.gdxy.controller;


import com.lablzh.gdxy.component.UserDetailsUtil;
import com.lablzh.gdxy.entity.PushfreeProject;
import com.lablzh.gdxy.entity.Sciachievement;
import com.lablzh.gdxy.entity.practice.StudentPractice;
import com.lablzh.gdxy.repository.SciachievementRepository;
import com.lablzh.gdxy.service.SciachievementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sciachievement")
public class SciachievementController {
    @Autowired
    private UserDetailsUtil userDetailsUtil;
    @Autowired
    private SciachievementRepository sciachievementRepository;

    @Autowired
    private SciachievementService sciachievementService;


    /**
     * 条件查询课外科技成果 stuCode name major classes grade projectName teacher type audit
     * @param sciachievement
     * @param page
     * @return
     */
    @PreAuthorize("hasAnyRole('instructor')")
    @PostMapping("/querySciachievement")
    public Map getqueryAllSciachievement(@RequestBody(required = false) Sciachievement sciachievement,
                                         @RequestParam(name = "page", defaultValue = "0") Integer page) {
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("stuCode", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("major", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("classes", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("grade", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("projectName", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("teacher", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("audit", ExampleMatcher.GenericPropertyMatchers.contains());

        Example<Sciachievement> example = Example.of(sciachievement, exampleMatcher);
        Pageable designPage = PageRequest.of(page -1, 20, Sort.by(Sort.Direction.DESC, "id"));
        Page<Sciachievement> all = sciachievementRepository.findAll(example, designPage);
        Map<String, Object> data = new HashMap<>();
        data.put("totalPage", all.getTotalPages());
        data.put("total", all.getTotalElements());
        data.put("sciachievement", all.getContent());
        return data;
    }

    @GetMapping("/allsciachievement")
    public List<Sciachievement> getallsciachievement() {
        return sciachievementService.selectallsciachievement();
    }

    @PostMapping("/apply")
    public String applysciachievement(@RequestBody Sciachievement sciachievement) {
        sciachievement.setAudit("未审");
        return sciachievementService.addsciachievement(sciachievement);
    }

    @DeleteMapping("/delete")
    public String deletesciachievement(@RequestParam("id") int id) {

        String role = userDetailsUtil.getUserRole();
        String username = userDetailsUtil.getUserDetails();
        if (role.equals("ROLE_instructor")) {
            return sciachievementService.deletesciachievement(id);
        } else {
            if (username.equals(sciachievementService.selectsciachievement(id).getStuCode())) {
                return sciachievementService.deletesciachievement(id);
            } else {
                return "你无权删除！";
            }
        }

    }

    @PreAuthorize("hasAnyRole('instructor')")
    @RequestMapping("/audit")
    public String auditsciachievement(@RequestParam("id") int id, @RequestParam("audit") String audit) {
        return sciachievementService.auditsciachievement(id, audit);
    }

    @RequestMapping("/studentsciachievement")
    public List<Sciachievement> studentsciachievement(@RequestParam("studentcode") String studentcode) {
        if (userDetailsUtil.getUserDetails().equals(studentcode)) {
            return sciachievementService.selectstudentsciachievement(studentcode);
        } else {
            return null;
        }

    }

    @RequestMapping("/sciachievementbyaudit")
    public List<Sciachievement> sciachievementbyaudit(@RequestParam("audit") String audit) {
        return sciachievementService.selectsciachievementbyaudit(audit);
    }

    @GetMapping("/sciachievementByStuCodeAndAudit")
    public List<Sciachievement> sciachievementByStuCodeAndAudit(@RequestParam("stuCode") String stuCode,
                                                                @RequestParam("audit") String audit) {
        return sciachievementService.selectSciAchievementByStuCodeAndAudit(stuCode, audit);
    }
    /**
     * 下载报表
     *
     * @param request
     * @param response
     * @return
     * @throws FileNotFoundException
     */
    @PreAuthorize("hasAnyRole('teacher','admin','instructor')")
    @PostMapping("/downloadExcel")
    public void downloadExcel(@RequestBody Sciachievement sciachievement,
                              @RequestParam("sort") String sort,
                              HttpServletRequest request,
                              HttpServletResponse response) throws FileNotFoundException {
        sciachievementService.downloadExcel( sciachievement, request, sort, response);
    }


}
